package bricolot.exporter;

import java.io.FileWriter;
import java.util.Iterator;

import bricolot.Garage;
import bricolot.Moto;
import bricolot.Reparation;
import bricolot.Societe;
import bricolot.Voiture;

public class BricolotoExporterV2 extends AXmlExporter implements
		IBricolotoExporter {

	protected final StringBuffer buffer = new StringBuffer();

	protected FileWriter writer;

	public BricolotoExporterV2(String anXmlFilename) {
		super(anXmlFilename);
		this.println("<!DOCTYPE Societe SYSTEM '" + anXmlFilename + ".dtd'>");
	}

	@Override
	public void visitSociete(Societe aSociete) {
		this.print("<Societe ");
		this.print("nom=\"");
		this.print(aSociete.getNom());
		this.print("\" ");
		this.print("adresse=\"");
		this.print(aSociete.getAdresse());
		this.println("\" >");
		this.printNewLine();

		Iterator<Garage> garageIt = aSociete.getGarages().iterator();
		while (garageIt.hasNext()) {
			garageIt.next().accept(this);
		}

		this.printNewLine();
		this.println("</Societe>");

	}

	@Override
	public void visitGarage(Garage aGarage) {
		this.tab(2);
		this.print("<Garage ");
		this.print("nom=\"");
		this.print(aGarage.getNom());
		this.print("\" ");
		this.print("adresse=\"");
		this.print(aGarage.getAdresse());
		this.println("\" >");
		this.printNewLine();

		Iterator<Reparation> reparationIt = aGarage.getReparations().iterator();
		while (reparationIt.hasNext()) {
			reparationIt.next().accept(this);
		}

		this.printNewLine();
		this.tab(2);
		this.println("</Garage>");
	}

	public void addReparationInfo(Reparation aReparation) {
		this.print("date=\"");
		this.print(aReparation.getDate());
		this.print("\" ");
		this.print("motif=\"");
		this.print(aReparation.getMotif());
		this.print("\" ");
	}

	@Override
	public void visitVoiture(Voiture aVoiture) {
		this.tab(4);
		this.print("<Voiture ");
		this.addReparationInfo(aVoiture);
		this.print("ct=\"");
		this.print(aVoiture.getCt());
		this.println("\" />");
	}

	@Override
	public void visitMoto(Moto aMoto) {
		this.tab(4);
		this.print("<Moto ");
		this.addReparationInfo(aMoto);
		this.print("sd=\"");
		this.print(Boolean.valueOf(aMoto.isSd()).toString());
		this.println("\" />");
	}

	public String getContents() {
		return super.getContents();
	}

	public void closeStream() {
		super.closeStream();
	}
}
